﻿using NHibernate;
using NHibernate.Cfg;
using Gp.Examples.FullCalendar.Infrastructure;

namespace Gp.Examples.FullCalendar.Data.NHibernate
{
    public class NhSession : IMapper
    {
        private static ISessionFactory _factory;
        private ISession _session;

        public object CurrentSession
        {
            get
            {
                if (_session != null)
                    return _session;

                if (_factory == null)
                {
                    CreateSessionFactory();
                }
                
                _session = _factory.OpenSession();
                return _session;
            }
        }

        private void CreateSessionFactory()
        {
            var cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly(GetType().Assembly);

            _factory = cfg.BuildSessionFactory();        
        }
    }
}
